package com.lsx.logisticscore.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsx.logistics.response.CommonResp;
import com.lsx.logisticscore.entity.Product;
import com.lsx.logisticscore.mapper.ProductMapper;
import com.lsx.logisticscore.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductService productService;

    @RequestMapping("/add")
    public CommonResp<Integer> addProduct(@RequestBody String productParam){
        Product product = JSONObject.parseObject(productParam, Product.class);
        productMapper.insert(product);
        return new CommonResp<>(product.getProductId());
    }

    @RequestMapping("/remove")
    public CommonResp<Integer> removeProduct(String productId){
        return new CommonResp<>(productMapper.deleteById(productId));
    }

    @RequestMapping("/list")
    public CommonResp<Page<Product>> listProduct(Page<Product> page, String category){
        if ("".equals(category) || category == null){
            category = null;
        }
        return new CommonResp<>(productMapper.selectProductByCondition(page, category));
    }

    /**
     * 获取所有产品类别
     * @return
     */
    @RequestMapping("/listCategory")
    public CommonResp<List<String>> listCategory(){
        return new CommonResp<>(productMapper.selectProductCategory());
    }

    @RequestMapping("/update")
    public CommonResp<Integer> updateProduct(@RequestBody String productParam){
        Product product = JSONObject.parseObject(productParam, Product.class);
        return new CommonResp<>(productMapper.updateById(product));
    }

    @RequestMapping("/get7dayInSum")
    public CommonResp<List<Map<String, Object>>> get7dayInSum(String local){
        CommonResp<List<Map<String, Object>>> resp = new CommonResp<>();
        resp.setContent(productMapper.get7dayInSum("本地"));
        return resp;
    }

    @RequestMapping("/get7dayOutSum")
    public CommonResp<List<Map<String, Object>>> get7dayOutSum(){
        return new CommonResp<>(productMapper.get7dayOutSum());
    }

    @RequestMapping("/getTodayOutByCategory")
    public CommonResp<List<Map<String, Object>>> getTodayOutByCategory(String local){
        return new CommonResp<>(productMapper.getTodayOutByCategory("本地"));
    }

    @RequestMapping("/getTodayOutTop5")
    public CommonResp<List<Map<String, Object>>> getTodayOutTop5(String local){
        return new CommonResp<>(productMapper.getTodayOutTop5("本地"));
    }
    // 七日资金流水情况 待补充

    @RequestMapping("/getProductSum")
    public CommonResp<Integer> getProductSum(){
        return new CommonResp<>(productMapper.selectSum());
    }

    @RequestMapping("/getProductValue")
    public CommonResp<Double> getProductValue(){
        return new CommonResp<>(productMapper.selectValue());
    }

    @RequestMapping("/getHottestIn30Day")
    public CommonResp<Map<String, Object>> getHottest(){
        return new CommonResp<>(productMapper.selectHottest());
    }

    @RequestMapping("/getCountByType")
    public CommonResp<List<Map<String, Object>>> getCountByType(){
        QueryWrapper<List<Map<String, Object>>> wrapper = new QueryWrapper<>();
        wrapper.groupBy("type");
        return new CommonResp<>(productMapper.selectCountByType());
    }

    @RequestMapping("/getCountByPrice")
    public CommonResp<List<Map<String, Object>>> getCountByPrice(){
        return new CommonResp<>(productMapper.selectCountByPrice());
    }

}
